Using Nix Flakes
Key takeaways
- A project is a flake if it has a flake.nix and flake.lock file.
- Flakes are a system by which developers can share projects and packages.
- Flakes can be used to build packages, run programs, and declare development environments.
- With Nix flakes, you can install, run, or include a package that is not part of the standard package set in nixpkgs. This requires the target package to itself be a flake.
- Flakes can have up to 4 sections: description, inputs, outputs, and nixConfig. Reference the flake schema when developing your own flakes.
- To work correctly, a flake has to be tracked by git.
Getting Started
Create a file in the root of your project called flake.nix. Since writing a flake by hand is labourious and error prone, it's best to use an existing template (See Essential Flake Templates). For example,
Managing a Flake
nix flake update
- Updates your flake's lock file. Important if you are working on a flake that is a dependency of another.
Development Environments with Direnv
By combining flakes with direnv, you can enter isolated development environments by simply cd
ing into a directory.
Setup
- Ensure Nix and direnv are installed
- Add a
flake.nix
file with adevShells
output - Add
use flake .
to your.envrc
file, e.g.,echo "use flake ." > .envrc
- Issue
direnv allow
Essential Flake Templates
To initialize a flake from a template use the command
nix flake init -t <source>#<template>
nix flake init -t <source>#<template>
Below I list a few essential projects, but bany more can be found online, just search, e.g., "nix flake template for TypeScript".
C++, JavaScript, Python, Rust, Go
- https://github.com/DeterminateSystems/zero-to-nix/ Bash, C, Go, Rust, Python
- https://github.com/nixos/templates Node/Typescript & Precommit
- https://github.com/akirak/flake-templates Python & Terraform & Rust
- https://github.com/serokell/templates Rust
- https://github.com/ipetkov/crane
- https://github.com/yusdacra/nix-cargo-integration
Alternatively, copy and paste the snippet below into flake.nix and adjust to your needs. flake.nix
nix
# Pulled from https://all-dressed-programming.com/posts/nix-flake-composition/
{
description = "Good basic flake template";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = {
default = pkgs.hello;
};
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [deno];
};
}
);
}
# Pulled from https://all-dressed-programming.com/posts/nix-flake-composition/
{
description = "Good basic flake template";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
packages = {
default = pkgs.hello;
};
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [deno];
};
}
);
}
ESSENTIAL READING
- YouTube - OpenTechLab
- Composition of Nix Flakes
- Building a Yarn Project with Nix Flake
- Effortless dev environments with Nix and direnv
- Workstation Management With Nix Flakes: Jupyter Notebook Example
- Workstation Management With Nix Flakes: Build a Cmake C++ Package